package com.fhz.mvvmshopjava.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fhz.mvvmshopjava.R;
import com.fhz.mvvmshopjava.databinding.GoodsRvLayoutBinding;
import com.fhz.mvvmshopjava.databinding.RvLayoutBinding;
import com.fhz.mvvmshopjava.entity.Banner;
import com.fhz.mvvmshopjava.entity.Order;

import java.util.List;

/**
 * 时间:2022/5/31
 *
 * @author msfeng
 * 简述: TODO
 */
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {

  private List<Order> list;

    public OrderAdapter(List<Order> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_rv_layout, null);
        GoodsRvLayoutBinding bind = DataBindingUtil.bind(inflate);
        return new ViewHolder(bind);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.binding.setOrder(list.get(position));
        holder.binding.executePendingBindings();
        //添加库存
        holder.binding.orderNumAdd.setOnClickListener(v -> {
            orderClick.orderAdd(holder.getAdapterPosition());
        });

        //减少库存
        holder.binding.orderNumSub.setOnClickListener(v -> {
            orderClick.orderSub(holder.getAdapterPosition());
        });
    }


    public interface OrderClick{
        void orderAdd(int i);
        void orderSub(int i);
    }
    private OrderClick orderClick;

    public void setOrderClick(OrderClick orderClick) {
        this.orderClick = orderClick;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        GoodsRvLayoutBinding binding;
        public ViewHolder(GoodsRvLayoutBinding bind) {
            super(bind.getRoot());
            this.binding = bind;
        }
    }




}


